import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Created with Intellij IDEA.
 * Description:
 * User: 33013
 * Date: 2023-10-26
 * Time: 21:03
 */
@WebServlet("/Index")
public class IndexServlet extends HttpServlet {
    private static int amount = 0;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        if (session == null){
            resp.setStatus(200);
            resp.setContentType("text/html; charset=utf8");
            resp.getWriter().write("请先登录");
            return;
        }
        amount++;
        String username = (String) session.getAttribute("username");
        long time = (long) session.getAttribute("time");
        resp.setContentType("text/html; charset=utf8");
        System.out.println("username: " + username + " 上次登陆时间为"  + "time" + time);
        resp.getWriter().write("username: " + username + ", 上次登陆时间为"  + "time" + time + "登录次数：" + amount);
    }
}
